﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;

namespace curbside.Web.UI.WebControls
{
    public class VerifyCode : System.Web.UI.WebControls.Image
    {
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            base.ToolTip = "看不清，换一张";
        }

        /// <summary>
        /// 重写Render方法
        /// </summary>
        /// <param name="writer"></param>
        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            base.Render(writer);
            string jsEvent = "$(function(){var jobj = $('#" + this.ClientID + "');";
            jsEvent += "jobj.click(function(e){this.src='VerifyCode.aspx?t='+new Date().valueOf();});";
            jsEvent += "jobj.attr('src', 'VerifyCode.aspx?t='+new Date().valueOf());";
            jsEvent += "});";
            helper.AddStartupScript(ClientID, jsEvent, this.Page);
        }

        /// <summary>
        /// 校验用户输入的验证码是否正确
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public bool IsVerifyCodeValidate(string str)
        {
            //参数为空，返回false
            if (str == null || string.IsNullOrEmpty(str.Trim()))
                return false;

            //Session丢失也返回false
            if (HttpContext.Current.Session["VerifyCode"] == null)
            {
                return false;
            }

            //传入的验证码与Session中的值不一致，返回false
            if (HttpContext.Current.Session["VerifyCode"].ToString().ToLower() != str.ToLower())
            {
                return false;
            }

            return true;
        }
    }
}
